#include <vector>
#include <string>
#include <algorithm>

using std::vector;
using std::string;

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        vector<vector<int>> ret;
        std::sort(intervals.begin(), intervals.end(), [](vector<int>& x, vector<int>& y)->bool {
            return x[0] <= y[0];
            });
        ret.push_back(intervals[0]);
        for (int i = 1; i < intervals.size(); i++) {
            if (intervals[i][0] > ret.back()[1]) {
                ret.push_back(intervals[i]);
            }
            else {
                if (ret.back()[1] < intervals[i][1]) {
                    ret.back()[1] = intervals[i][1];
                }
            }
        }
        return ret;
    }
};